using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

public partial class News : System.Web.UI.Page
{
    private const int PAGESIZE = 6;

    public int PageNumber
    {
        get
        {
            if (ViewState["PageNumber"] != null)
                return Convert.ToInt32(ViewState["PageNumber"]);
            else
                return 0;
        }
        set
        {
            ViewState["PageNumber"] = value;
        }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        rptPages1.ItemCommand += new RepeaterCommandEventHandler(rptPages_ItemCommand);
        rptPages2.ItemCommand += new RepeaterCommandEventHandler(rptPages_ItemCommand);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        _header.Text = "News archive" + GetContext();
        if (!IsPostBack)
            LoadData();
    }

    protected string GetNewsAsHtml(string newsMessage)
    {
        return StringExtensions.RenderAsHtml(newsMessage);
    }

    protected string GetNewsDate(DateTime newsDate)
    {
        return TimespanExtensions.HowLongAgo(newsDate);
    }

    private string GetContext()
    {
        string context = Request.QueryString["context"];
        if (context == null)
            return "";
        return " for " + context;
    }

    private void LoadData()
    {
        int playerId = -1;
        string playerIdAsString = Request.QueryString["playerId"];
        if (playerIdAsString != null)
            playerId = Convert.ToInt32(playerIdAsString);

        int predictionId = -1;
        string predictionIdAsString = Request.QueryString["predictionId"];
        if (predictionIdAsString != null)
            predictionId = Convert.ToInt32(predictionIdAsString);

        OleDbConnection cn = new OleDbConnection(ConfigurationManager.ConnectionStrings["ForecastDB"].ConnectionString);
        cn.Open();

        string sql = "SELECT * FROM [NewsItem] ";
        if (playerId != -1)
            sql += " WHERE [Player_id] = " + playerId + " ";
        else if (predictionId != -1)
            sql += " WHERE [Prediction_id] = " + predictionId + " AND [Should_be_shown_in_main_news] <> 0 ";
        else
            sql += " WHERE [Should_be_shown_in_main_news] <> 0 ";
        sql += " ORDER BY [News_date] DESC ";
        
        
        OleDbDataAdapter da = new OleDbDataAdapter(sql, cn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        cn.Close();

        PagedDataSource pgitems = new PagedDataSource();
        DataView dv = new DataView(dt);
        pgitems.DataSource = dv;
        pgitems.AllowPaging = true;
        pgitems.PageSize = PAGESIZE;
        pgitems.CurrentPageIndex = PageNumber;
        if (pgitems.PageCount > 1)
        {
            rptPages1.Visible = true;
            rptPages2.Visible = true;
            ArrayList pages = new ArrayList();
            for (int i = 0; i < pgitems.PageCount; i++)
                pages.Add((i + 1).ToString());
            rptPages1.DataSource = pages;
            rptPages1.DataBind();
            rptPages2.DataSource = pages;
            rptPages2.DataBind();
        }
        else
        {
            rptPages1.Visible = false;
            rptPages2.Visible = false;
        }
        rptItems.DataSource = pgitems;
        rptItems.DataBind();
    }

   void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
   {
      PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
      LoadData();
   }
}
